<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>GORM基本操作 | anyang的博客</title>
    <meta name="description" content="千里之行,始于足下">
    <meta name="generator" content="VuePress 1.3.1">
    <link rel="icon" href="/img/favicon.ico">
  <script charset="utf-8" src="/js/main.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.css">
  <meta name="keywords" content="福小林,奔奔,ourLang,CentOS,JAVA,vue组件,lsdCloud">
  <script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-146891701-1"></script>
  <script>window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'UA-146891701-1');</script>
  <script>var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?56eae8eec590ccaef1d5ff99d766f315";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();</script>
    
    <link rel="preload" href="/assets/css/0.styles.9fff9873.css" as="style"><link rel="preload" href="/assets/js/app.1bc80adb.js" as="script"><link rel="preload" href="/assets/js/2.7c0608ab.js" as="script"><link rel="preload" href="/assets/js/95.1b379a5b.js" as="script"><link rel="prefetch" href="/assets/js/10.81caa2ab.js"><link rel="prefetch" href="/assets/js/100.05bb269d.js"><link rel="prefetch" href="/assets/js/101.3e18b4d6.js"><link rel="prefetch" href="/assets/js/102.c7d7ed9c.js"><link rel="prefetch" href="/assets/js/103.4c771ce7.js"><link rel="prefetch" href="/assets/js/104.a2acd367.js"><link rel="prefetch" href="/assets/js/105.63399350.js"><link rel="prefetch" href="/assets/js/106.8a4744cc.js"><link rel="prefetch" href="/assets/js/107.7739d796.js"><link rel="prefetch" href="/assets/js/108.8008ae33.js"><link rel="prefetch" href="/assets/js/109.01c63898.js"><link rel="prefetch" href="/assets/js/11.7b837560.js"><link rel="prefetch" href="/assets/js/110.ad01aca9.js"><link rel="prefetch" href="/assets/js/111.3d74db70.js"><link rel="prefetch" href="/assets/js/112.67f55c34.js"><link rel="prefetch" href="/assets/js/113.016cd3c0.js"><link rel="prefetch" href="/assets/js/114.860e4b2b.js"><link rel="prefetch" href="/assets/js/115.b2f26258.js"><link rel="prefetch" href="/assets/js/116.8bea67b4.js"><link rel="prefetch" href="/assets/js/117.20682843.js"><link rel="prefetch" href="/assets/js/118.86a22e16.js"><link rel="prefetch" href="/assets/js/119.ad518bea.js"><link rel="prefetch" href="/assets/js/12.8fd78714.js"><link rel="prefetch" href="/assets/js/120.f97dd86f.js"><link rel="prefetch" href="/assets/js/121.c5601b4a.js"><link rel="prefetch" href="/assets/js/122.2889645a.js"><link rel="prefetch" href="/assets/js/13.ebf26820.js"><link rel="prefetch" href="/assets/js/14.b92dca09.js"><link rel="prefetch" href="/assets/js/15.b3dcef45.js"><link rel="prefetch" href="/assets/js/16.50378990.js"><link rel="prefetch" href="/assets/js/17.056bba2b.js"><link rel="prefetch" href="/assets/js/18.9047bb38.js"><link rel="prefetch" href="/assets/js/19.ab53b0fe.js"><link rel="prefetch" href="/assets/js/20.fe0b8af0.js"><link rel="prefetch" href="/assets/js/21.20a702f3.js"><link rel="prefetch" href="/assets/js/22.0ee08d29.js"><link rel="prefetch" href="/assets/js/23.02039a9a.js"><link rel="prefetch" href="/assets/js/24.363d3d74.js"><link rel="prefetch" href="/assets/js/25.dd6b101e.js"><link rel="prefetch" href="/assets/js/26.20493714.js"><link rel="prefetch" href="/assets/js/27.708723e5.js"><link rel="prefetch" href="/assets/js/28.4561834a.js"><link rel="prefetch" href="/assets/js/29.15cfcf5f.js"><link rel="prefetch" href="/assets/js/3.8d39d40c.js"><link rel="prefetch" href="/assets/js/30.04ab69de.js"><link rel="prefetch" href="/assets/js/31.b11c4302.js"><link rel="prefetch" href="/assets/js/32.81ae74ee.js"><link rel="prefetch" href="/assets/js/33.ddf09cc0.js"><link rel="prefetch" href="/assets/js/34.bc3549f7.js"><link rel="prefetch" href="/assets/js/35.4a0e0ebd.js"><link rel="prefetch" href="/assets/js/36.961a1c50.js"><link rel="prefetch" href="/assets/js/37.d8658de5.js"><link rel="prefetch" href="/assets/js/38.f76063aa.js"><link rel="prefetch" href="/assets/js/39.73edd08f.js"><link rel="prefetch" href="/assets/js/4.916af9bf.js"><link rel="prefetch" href="/assets/js/40.72314bff.js"><link rel="prefetch" href="/assets/js/41.c5eb1147.js"><link rel="prefetch" href="/assets/js/42.07aeafbb.js"><link rel="prefetch" href="/assets/js/43.c4a652ba.js"><link rel="prefetch" href="/assets/js/44.91fbe22b.js"><link rel="prefetch" href="/assets/js/45.6eecf0c4.js"><link rel="prefetch" href="/assets/js/46.81b7f108.js"><link rel="prefetch" href="/assets/js/47.b4468ab3.js"><link rel="prefetch" href="/assets/js/48.df205f23.js"><link rel="prefetch" href="/assets/js/49.7df59fa3.js"><link rel="prefetch" href="/assets/js/5.4fcd540a.js"><link rel="prefetch" href="/assets/js/50.0dc2fc5b.js"><link rel="prefetch" href="/assets/js/51.1ffabe54.js"><link rel="prefetch" href="/assets/js/52.2269d284.js"><link rel="prefetch" href="/assets/js/53.81b23843.js"><link rel="prefetch" href="/assets/js/54.b3249def.js"><link rel="prefetch" href="/assets/js/55.50dcf2af.js"><link rel="prefetch" href="/assets/js/56.c399c272.js"><link rel="prefetch" href="/assets/js/57.024f1dbb.js"><link rel="prefetch" href="/assets/js/58.680c6cf4.js"><link rel="prefetch" href="/assets/js/59.2291e9d7.js"><link rel="prefetch" href="/assets/js/6.7610d1d4.js"><link rel="prefetch" href="/assets/js/60.e59a26c1.js"><link rel="prefetch" href="/assets/js/61.ff5c64e4.js"><link rel="prefetch" href="/assets/js/62.7d97062c.js"><link rel="prefetch" href="/assets/js/63.6456dc7e.js"><link rel="prefetch" href="/assets/js/64.0b055680.js"><link rel="prefetch" href="/assets/js/65.9f25601c.js"><link rel="prefetch" href="/assets/js/66.cd973084.js"><link rel="prefetch" href="/assets/js/67.9b8f9716.js"><link rel="prefetch" href="/assets/js/68.d6c1869a.js"><link rel="prefetch" href="/assets/js/69.0a3688dd.js"><link rel="prefetch" href="/assets/js/7.9ebbd956.js"><link rel="prefetch" href="/assets/js/70.22f48891.js"><link rel="prefetch" href="/assets/js/71.9eb4fda9.js"><link rel="prefetch" href="/assets/js/72.ca4fa80c.js"><link rel="prefetch" href="/assets/js/73.17d1ab37.js"><link rel="prefetch" href="/assets/js/74.139691db.js"><link rel="prefetch" href="/assets/js/75.08d0560f.js"><link rel="prefetch" href="/assets/js/76.61078c10.js"><link rel="prefetch" href="/assets/js/77.4482adcc.js"><link rel="prefetch" href="/assets/js/78.166f2c37.js"><link rel="prefetch" href="/assets/js/79.2c7d6b79.js"><link rel="prefetch" href="/assets/js/8.6f8916c2.js"><link rel="prefetch" href="/assets/js/80.80445c3f.js"><link rel="prefetch" href="/assets/js/81.14e3bae8.js"><link rel="prefetch" href="/assets/js/82.7dac687f.js"><link rel="prefetch" href="/assets/js/83.1dd2e78e.js"><link rel="prefetch" href="/assets/js/84.23b759de.js"><link rel="prefetch" href="/assets/js/85.7574f69f.js"><link rel="prefetch" href="/assets/js/86.102dac61.js"><link rel="prefetch" href="/assets/js/87.8f5d639e.js"><link rel="prefetch" href="/assets/js/88.e564cbbe.js"><link rel="prefetch" href="/assets/js/89.cd66eb78.js"><link rel="prefetch" href="/assets/js/9.baa162db.js"><link rel="prefetch" href="/assets/js/90.685c6878.js"><link rel="prefetch" href="/assets/js/91.b8d1b7ea.js"><link rel="prefetch" href="/assets/js/92.1faa374d.js"><link rel="prefetch" href="/assets/js/93.cf5ff33c.js"><link rel="prefetch" href="/assets/js/94.8a02aefb.js"><link rel="prefetch" href="/assets/js/96.9fd67185.js"><link rel="prefetch" href="/assets/js/97.c5a6a5a1.js"><link rel="prefetch" href="/assets/js/98.9a231411.js"><link rel="prefetch" href="/assets/js/99.5b344cab.js">
    <link rel="stylesheet" href="/assets/css/0.styles.9fff9873.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">anyang的博客</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
  主页
</a></div><div class="nav-item"><a href="/study-guide/introduction.html" class="nav-link">
  学习指南
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术分类" class="dropdown-title"><span class="title">技术分类</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/java/introduction.html" class="nav-link">
  Java
</a></li><li class="dropdown-item"><!----> <a href="/go/introduction.html" class="nav-link">
  Go
</a></li><li class="dropdown-item"><!----> <a href="/sql/introduction.html" class="nav-link">
  SQL
</a></li><li class="dropdown-item"><!----> <a href="/fhir/introduction.html" class="nav-link">
  FHIR
</a></li></ul></div></div><div class="nav-item"><a href="/blog/introduction.html" class="nav-link">
  个人博客
</a></div><div class="nav-item"><a href="/tool/developmentBox.html" class="nav-link">
  开发百宝箱
</a></div><div class="nav-item"><a href="/project/introduction.html" class="nav-link">
  项目分享
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="了解更多" class="dropdown-title"><span class="title">了解更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://wpa.qq.com/msgrd?v=3&amp;uin=1300378587&amp;site=qq&amp;menu=yes" target="_blank" rel="noopener noreferrer" class="nav-link external">
  其他合作
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="http://www.lsdcloud.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  友情网站
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://github.com/ourlang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/anlexanyang" target="_blank" rel="noopener noreferrer" class="nav-link external">
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/qq_37493556" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/linkExchanges/introduction.html" class="nav-link">
  友情链接
</a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/go/introduction.html" class="sidebar-link">Go语言</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go教程</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Go Web框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Go工具中间件</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/go/middleware/go-coding-standard.html" class="sidebar-link">Golang语言编码规范</a></li><li><a href="/go/middleware/go-get命令详解.html" class="sidebar-link">go get命令详解</a></li><li><a href="/go/middleware/go-mysql.html" class="sidebar-link">Go语言操作Mysql</a></li><li><a href="/go/middleware/go-redis.html" class="sidebar-link">go-redis文档</a></li><li><a href="/go/middleware/go-swagger.html" class="sidebar-link">go-swagger安装及使用</a></li><li><a href="/go/middleware/go-viper.html" class="sidebar-link">viper操作文档</a></li><li><a href="/go/middleware/Goland的自定义快捷键配置.html" class="sidebar-link">配置Goland的自定义快捷键</a></li><li><a href="/go/middleware/Golang的安装配置golint.html" class="sidebar-link">Golang的安装配置golint</a></li><li><a href="/go/middleware/goproxy.html" class="sidebar-link">GoLand配置GOPROXY代理</a></li><li><a href="/go/middleware/gorm.html" class="active sidebar-link">GORM基本操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_1-安装" class="sidebar-link">1 安装</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_2-数据库连接" class="sidebar-link">2 数据库连接</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_3-gorm模型定义" class="sidebar-link">3 GORM模型定义</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_3-1-模型概念说明" class="sidebar-link">3.1  模型概念说明</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_3-2-gorm模型定义" class="sidebar-link">3.2 GORM模型定义</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_3-3-gorm模型标签" class="sidebar-link">3.3 GORM模型标签</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_4-根据gorm模型自动创建表" class="sidebar-link">4 根据GORM模型自动创建表</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_5-crud使用准备工作" class="sidebar-link">5 CRUD使用准备工作</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_6-插入数据" class="sidebar-link">6 插入数据</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_7-更新数据" class="sidebar-link">7 更新数据</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_7-1-更新修改字段" class="sidebar-link">7.1 更新修改字段</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_7-2-更新所有字段" class="sidebar-link">7.2 更新所有字段</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_7-3-更新选定字段" class="sidebar-link">7.3 更新选定字段</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_7-4-无-hooks-更新" class="sidebar-link">7.4 无 Hooks 更新</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_7-5-批量更新" class="sidebar-link">7.5 批量更新</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_7-6-使用sql表达式更新" class="sidebar-link">7.6 使用SQL表达式更新</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_8-删除数据" class="sidebar-link">8 删除数据</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_8-1-删除记录" class="sidebar-link">8.1 删除记录</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_8-2-批量删除" class="sidebar-link">8.2 批量删除</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_8-3-物理删除" class="sidebar-link">8.3 物理删除</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_9-查询数据" class="sidebar-link">9 查询数据</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_9-1-查询方法" class="sidebar-link">9.1 查询方法</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_9-2-查询错误处理" class="sidebar-link">9.2 查询错误处理</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_9-3-where条件查询" class="sidebar-link">9.3 Where条件查询</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_9-4-not条件" class="sidebar-link">9.4 Not条件</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_9-5-or条件" class="sidebar-link">9.5 Or条件</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_9-6-select子句" class="sidebar-link">9.6 select子句</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_9-7-order排序" class="sidebar-link">9.7 order排序</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_9-8-limit-count" class="sidebar-link">9.8 Limit &amp; Count</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_9-9-group-having" class="sidebar-link">9.9 Group &amp; Having</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_9-10-连接join" class="sidebar-link">9.10 连接join</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_9-11-直接执行sql语句" class="sidebar-link">9.11 直接执行sql语句</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_10-sql生成器" class="sidebar-link">10 SQL生成器</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_10-1-执行原生sql" class="sidebar-link">10.1 执行原生SQL</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_10-2-sql-row和sql-rows" class="sidebar-link">10.2 sql.Row和sql.Rows</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_10-3-将sql-rows扫描至model" class="sidebar-link">10.3 将sql.Rows扫描至model</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_11-事务处理" class="sidebar-link">11 事务处理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_11-1-事务流程说明" class="sidebar-link">11.1 事务流程说明</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_11-2-事务（手动控制）" class="sidebar-link">11.2 事务（手动控制）</a></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_11-3-事务示例" class="sidebar-link">11.3 事务示例</a></li></ul></li><li class="sidebar-sub-header"><a href="/go/middleware/gorm.html#_12-gorm更多资料" class="sidebar-link">12 GORM更多资料</a></li></ul></li><li><a href="/go/middleware/json-iterator.html" class="sidebar-link">json-iterator使用</a></li><li><a href="/go/middleware/package.html" class="sidebar-link">Go语言包和包管理</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="gorm基本操作"><a href="#gorm基本操作" class="header-anchor">#</a> GORM基本操作</h1> <div class="custom-block tip"><p class="custom-block-title">概览</p> <ul><li>全功能 ORM (无限接近)</li> <li>关联 (Has One, Has Many, Belongs To, Many To Many, 多态)</li> <li>钩子 (在创建/保存/更新/删除/查找之前或之后)</li> <li>预加载</li> <li>事务</li> <li>复合主键</li> <li>SQL 生成器</li> <li>数据库自动迁移</li> <li>自定义日志</li> <li>可扩展性, 可基于 GORM 回调编写插件</li> <li>所有功能都被测试覆盖</li> <li>开发者友好</li></ul></div> <h2 id="_1-安装"><a href="#_1-安装" class="header-anchor">#</a> 1 安装</h2> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">go</span> get <span class="token operator">-</span>u github<span class="token punctuation">.</span>com<span class="token operator">/</span>jinzhu<span class="token operator">/</span>gorm
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_2-数据库连接"><a href="#_2-数据库连接" class="header-anchor">#</a> 2 数据库连接</h2> <blockquote><p><a href="https://gorm.io/zh_CN/docs/connecting_to_the_database.html" target="_blank" rel="noopener noreferrer">更多GORM数据库连接说明<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;github.com/jinzhu/gorm&quot;</span>
	<span class="token boolean">_</span> <span class="token string">&quot;github.com/jinzhu/gorm/dialects/mysql&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">var</span> <span class="token punctuation">(</span>
	<span class="token comment">// 定义一个全局对象db</span>
	db <span class="token operator">*</span>gorm<span class="token punctuation">.</span>DB
	<span class="token comment">//定义数据库连接的相关参数值</span>
	<span class="token comment">//连接数据库的用户名</span>
	userName <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;root&quot;</span>
	<span class="token comment">//连接数据库的密码</span>
	password <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;admin&quot;</span>
	<span class="token comment">//连接数据库的地址</span>
	ipAddress <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;127.0.0.1&quot;</span>
	<span class="token comment">//连接数据库的端口号</span>
	port <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">3306</span>
	<span class="token comment">//连接数据库的具体数据库名称</span>
	dbName <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;go_test&quot;</span>
	<span class="token comment">//连接数据库的编码格式</span>
	charset <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;utf8mb4&quot;</span>
	<span class="token comment">//想要能正确的处理 time.Time，你需要添加 parseTime 参数。</span>
	parseTime <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;True&quot;</span>
	<span class="token comment">//设置时间的位置</span>
	loc <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;Local&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>err <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//dsn=&quot;root:admin@tcp(127.0.0.1:3306)/go_test?charset=utf8mb4&amp;parseTime=True&amp;loc=Local&quot;</span>
	dsn <span class="token operator">:=</span> fmt<span class="token punctuation">.</span><span class="token function">Sprintf</span><span class="token punctuation">(</span><span class="token string">&quot;%s:%s@tcp(%s:%d)/%s?charset=%s&amp;parseTime=%s&amp;loc=%s&quot;</span><span class="token punctuation">,</span> userName<span class="token punctuation">,</span> password<span class="token punctuation">,</span> ipAddress<span class="token punctuation">,</span> port<span class="token punctuation">,</span> dbName<span class="token punctuation">,</span> charset<span class="token punctuation">,</span> parseTime<span class="token punctuation">,</span> loc<span class="token punctuation">)</span>
	db<span class="token punctuation">,</span> err <span class="token operator">=</span> gorm<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span><span class="token string">&quot;mysql&quot;</span><span class="token punctuation">,</span> dsn<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;连接数据库异常,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>
	<span class="token comment">//设置最大的空闲连接数</span>
	db<span class="token punctuation">.</span><span class="token function">DB</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">SetMaxIdleConns</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
	<span class="token comment">// 设置最大连接数</span>
	db<span class="token punctuation">.</span><span class="token function">DB</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">SetMaxOpenConns</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span>
	<span class="token comment">//自动生成数据库表 ，生成的表明默认是struct名称的复数形式。如：user -》users</span>
	<span class="token comment">// 如果不想是复数形式，需设定：</span>
	db<span class="token punctuation">.</span><span class="token function">SingularTable</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
	<span class="token comment">//defer db.Close()</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;连接数据库成功！&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br></div></div><h2 id="_3-gorm模型定义"><a href="#_3-gorm模型定义" class="header-anchor">#</a> 3 GORM模型定义</h2> <h3 id="_3-1-模型概念说明"><a href="#_3-1-模型概念说明" class="header-anchor">#</a> 3.1  模型概念说明</h3> <div class="custom-block tip"><p class="custom-block-title">模型概念</p> <p>ORM框架操作数据库都需要预先定义模型，模型可以理解成数据模型，作为操作数据库的媒介。</p> <ul><li>从数据库读取的数据会先保存到预先定义的模型对象，然后我们就可以从模型对象得到我们想要的数据。</li> <li>插入数据到数据库也是先新建一个模型对象，然后把想要保存的数据先保存到模型对象，然后把模型对象保存到数据库。</li></ul></div> <p>在<code>golang</code>中<code>gorm</code>模型定义是通过<code>struct</code>实现的，这样我们就可以通过<code>gorm</code>库实现<code>struct</code>类型和<code>mysql</code>表数据的映射。
<code>gorm</code>负责将对模型的读写操作翻译成<code>sql</code>语句，然后<code>gorm</code>再把数据库执行<code>sql</code>语句后返回的结果转化为我们定义的模型对象。</p> <h3 id="_3-2-gorm模型定义"><a href="#_3-2-gorm模型定义" class="header-anchor">#</a> 3.2 GORM模型定义</h3> <blockquote><p><code>gorm</code>模型定义主要就是在<code>struct</code>类型定义的基础上增加字段标签说明实现，下面看个完整的例子。假如有个用户表，表结构如下</p></blockquote> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span><span class="token keyword">user</span><span class="token punctuation">`</span> <span class="token punctuation">(</span>
   <span class="token punctuation">`</span>id<span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span>
   <span class="token punctuation">`</span>name<span class="token punctuation">`</span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
   <span class="token punctuation">`</span>age<span class="token punctuation">`</span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
   <span class="token punctuation">`</span>create_time<span class="token punctuation">`</span> <span class="token keyword">datetime</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span>
   <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token punctuation">`</span>id<span class="token punctuation">`</span><span class="token punctuation">)</span>
 <span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>模型定义如下</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Id  <span class="token builtin">int</span>   <span class="token string">`gorm:&quot;primary_key&quot;`</span> <span class="token comment">//表字段名为：id,主键</span>
	Name <span class="token builtin">string</span>   <span class="token string">`gorm:&quot;not null&quot;`</span> <span class="token comment">//模型标签 name字段不能为空</span>
	Age  <span class="token builtin">int</span>     <span class="token comment">//年龄age字段</span>
    <span class="token comment">//对应表中的create_time字段并且不为空</span>
	CreateTime  time<span class="token punctuation">.</span>Time	<span class="token string">`gorm:&quot;column:create_time;not null&quot;`</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_3-3-gorm模型标签"><a href="#_3-3-gorm模型标签" class="header-anchor">#</a> 3.3 GORM模型标签</h3> <blockquote><p>通过上面的例子，大家看到可以通过类似<code>gorm:&quot;column:create_time&quot;</code>这样的标签定义语法，定义<code>struct</code>字段的列名（表字段名）。</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//标签定义部分，多个标签定义可以使用分号（;）分隔,</span>
<span class="token comment">//比如`gorm:&quot;column:create_time;not null&quot;`</span>
<span class="token string">`gorm`</span>标签语法：<span class="token string">`gorm:&quot;标签定义&quot;`</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>常用标签如下，更多标签请参考官网</strong><a href="https://gorm.io/zh_CN/docs/models.html" target="_blank" rel="noopener noreferrer">模型定义<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <table><thead><tr><th style="text-align:center;">标签</th> <th>说明</th> <th>例子</th></tr></thead> <tbody><tr><td style="text-align:center;">COLUMN</td> <td>指定列名</td> <td>gorm:&quot;column:create_time&quot;</td></tr> <tr><td style="text-align:center;">PRIMARY_KEY</td> <td>指定主键</td> <td>gorm:&quot;column:id; PRIMARY_KEY&quot;</td></tr> <tr><td style="text-align:center;">NOT NULL</td> <td>将列指定为非 NULL</td> <td>gorm:&quot;not null&quot;</td></tr> <tr><td style="text-align:center;">-</td> <td>忽略字段</td> <td>gorm:&quot;-&quot; 可以忽略struct字段，被忽略的字段不参与gorm的读写操作</td></tr></tbody></table> <h2 id="_4-根据gorm模型自动创建表"><a href="#_4-根据gorm模型自动创建表" class="header-anchor">#</a> 4 根据GORM模型自动创建表</h2> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//创建一个和数据库表结构一样的机构体</span>
<span class="token comment">//gorm 用 tag 的方式来标识 mysql 里面的约束</span>
<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Id  <span class="token builtin">int</span>   <span class="token string">`gorm:&quot;primary_key&quot;`</span>
	Name <span class="token builtin">string</span>   <span class="token string">`gorm:&quot;not null&quot;`</span>
	Age  <span class="token builtin">int</span>
	CreateTime  time<span class="token punctuation">.</span>Time	<span class="token string">`gorm:&quot;column:create_time;not null&quot;`</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
       <span class="token comment">//连接数据库方法请查看上面第二点</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//如果数据库中没有user表就创建</span>
	<span class="token keyword">if</span> <span class="token operator">!</span>db<span class="token punctuation">.</span><span class="token function">HasTable</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//直接通过 db.CreateTable 就可以创建表了，非常方便，</span>
<span class="token comment">//还可以通过 db.Set 设置一些额外的表属性,</span>
		<span class="token keyword">if</span> err <span class="token operator">:=</span> db<span class="token punctuation">.</span><span class="token function">Set</span><span class="token punctuation">(</span><span class="token string">&quot;gorm:table_options&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;ENGINE=InnoDB DEFAULT CHARSET=utf8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">CreateTable</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Error<span class="token punctuation">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
			<span class="token function">panic</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h2 id="_5-crud使用准备工作"><a href="#_5-crud使用准备工作" class="header-anchor">#</a> 5 CRUD使用准备工作</h2> <blockquote><p>下面的CRUD公用的代码是数据库连接和结构体声明，代码如下</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;github.com/jinzhu/gorm&quot;</span>
	<span class="token boolean">_</span> <span class="token string">&quot;github.com/jinzhu/gorm/dialects/mysql&quot;</span>
	<span class="token string">&quot;time&quot;</span>
<span class="token punctuation">)</span>
<span class="token keyword">var</span> <span class="token punctuation">(</span>
	<span class="token comment">// 定义一个全局对象db</span>
	db <span class="token operator">*</span>gorm<span class="token punctuation">.</span>DB
	<span class="token comment">//定义数据库连接的相关参数值</span>
	<span class="token comment">//连接数据库的用户名</span>
	userName <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;root&quot;</span>
	<span class="token comment">//连接数据库的密码</span>
	password <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;admin&quot;</span>
	<span class="token comment">//连接数据库的地址</span>
	ipAddress <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;127.0.0.1&quot;</span>
	<span class="token comment">//连接数据库的端口号</span>
	port <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">3306</span>
	<span class="token comment">//连接数据库的具体数据库名称</span>
	dbName <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;go_test&quot;</span>
	<span class="token comment">//连接数据库的编码格式</span>
	charset <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;utf8mb4&quot;</span>
	<span class="token comment">//想要能正确的处理 time.Time，你需要添加 parseTime 参数。</span>
	parseTime <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;True&quot;</span>
	<span class="token comment">//设置时间的位置</span>
	loc <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;Local&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>err <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//dsn=&quot;root:admin@tcp(127.0.0.1:3306)/go_test?charset=utf8mb4&amp;parseTime=True&amp;loc=Local&quot;</span>
	dsn <span class="token operator">:=</span> fmt<span class="token punctuation">.</span><span class="token function">Sprintf</span><span class="token punctuation">(</span><span class="token string">&quot;%s:%s@tcp(%s:%d)/%s?charset=%s&amp;parseTime=%s&amp;loc=%s&quot;</span><span class="token punctuation">,</span> userName<span class="token punctuation">,</span> password<span class="token punctuation">,</span> ipAddress<span class="token punctuation">,</span> port<span class="token punctuation">,</span> dbName<span class="token punctuation">,</span> charset<span class="token punctuation">,</span> parseTime<span class="token punctuation">,</span> loc<span class="token punctuation">)</span>
	db<span class="token punctuation">,</span> err <span class="token operator">=</span> gorm<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span><span class="token string">&quot;mysql&quot;</span><span class="token punctuation">,</span> dsn<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;连接数据库异常,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>
	<span class="token comment">//设置最大的空闲连接数</span>
	db<span class="token punctuation">.</span><span class="token function">DB</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">SetMaxIdleConns</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
	<span class="token comment">// 设置最大连接数</span>
	db<span class="token punctuation">.</span><span class="token function">DB</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">SetMaxOpenConns</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span>
	<span class="token comment">//自动生成数据库表 ，生成的表明默认是struct名称的复数形式。如：user -》users</span>
	<span class="token comment">// 如果不想是复数形式，需设定：</span>
	db<span class="token punctuation">.</span><span class="token function">SingularTable</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
	<span class="token comment">//defer db.Close()</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;连接数据库成功！&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>

<span class="token comment">//创建一个和数据库表结构一样的机构体</span>
<span class="token comment">//gorm 用 tag 的方式来标识 mysql 里面的约束</span>
<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Id         <span class="token builtin">int</span>    <span class="token string">`gorm:&quot;primary_key&quot;`</span>
	Name       <span class="token builtin">string</span> <span class="token string">`gorm:&quot;not null&quot;`</span>
	Age        <span class="token builtin">int</span>
	CreateTime time<span class="token punctuation">.</span>Time <span class="token string">`gorm:&quot;column:create_time;not null&quot;`</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br></div></div><h2 id="_6-插入数据"><a href="#_6-插入数据" class="header-anchor">#</a> 6 插入数据</h2> <blockquote><p>更多用法请参考官网<a href="https://gorm.io/zh_CN/docs/create.html" target="_blank" rel="noopener noreferrer">插入数据<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//插入数据</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>user <span class="token operator">*</span>User<span class="token punctuation">)</span> <span class="token function">InsertUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>err <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//这里使用了Table()函数，如果你没有指定全局表名禁用复数，或者是表名跟结构体名不一样的时候</span>
	<span class="token comment">//你可以自己在sql中指定表名。这里是示例，本例中这个函数可以去除。db.Create(user)也可以</span>
	createDb <span class="token operator">:=</span> db<span class="token punctuation">.</span><span class="token function">Table</span><span class="token punctuation">(</span><span class="token string">&quot;user&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span>
	err <span class="token operator">=</span> createDb<span class="token punctuation">.</span>Error
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;新增数据错误,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>
<span class="token comment">//主函数调用</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//连接数据库</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
    <span class="token comment">//定义需要新增的user结构体</span>
	user <span class="token operator">:=</span> User<span class="token punctuation">{</span>Name<span class="token punctuation">:</span> <span class="token string">&quot;李四&quot;</span><span class="token punctuation">,</span> Age<span class="token punctuation">:</span> <span class="token number">16</span><span class="token punctuation">,</span> CreateTime<span class="token punctuation">:</span> time<span class="token punctuation">.</span><span class="token function">Now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">}</span>
    <span class="token comment">//调用user表的插入操作</span>
	err <span class="token operator">=</span> user<span class="token punctuation">.</span><span class="token function">InsertUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;新增数据错误,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//新增结束后会返回对应结构的所有数据，比如id</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;新增数据成功，新增user机构体主键是&quot;</span><span class="token punctuation">,</span> user<span class="token punctuation">.</span>Id<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><h2 id="_7-更新数据"><a href="#_7-更新数据" class="header-anchor">#</a> 7 更新数据</h2> <blockquote><p>更多用法请参考官网<a href="https://gorm.io/zh_CN/docs/update.html" target="_blank" rel="noopener noreferrer">更新数据<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></blockquote> <h3 id="_7-1-更新修改字段"><a href="#_7-1-更新修改字段" class="header-anchor">#</a> 7.1 更新修改字段</h3> <p>如果你只希望更新指定字段，可以使用Update或者Updates</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//更新数据user表</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>user <span class="token operator">*</span>User<span class="token punctuation">)</span> <span class="token function">UpdateUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span>err <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//注意，Model方法必须要和Update方法一起使用 注意：db.Update(user)这样更新数据不生效</span>
<span class="token comment">//使用效果相当于Model中设置更新的主键key(如果没有where指定,那么默认更新的key为id),Update中设置更新的值</span>
<span class="token comment">//如果Model中没有指定id值，且也没有指定where条件，那么将更新全表</span>
	update <span class="token operator">:=</span>db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Update</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span>
	err <span class="token operator">=</span> update<span class="token punctuation">.</span>Error
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;更新数据错误,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	user <span class="token operator">:=</span> User<span class="token punctuation">{</span>Id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> Name<span class="token punctuation">:</span> <span class="token string">&quot;赵六&quot;</span><span class="token punctuation">,</span>Age<span class="token punctuation">:</span><span class="token number">28</span><span class="token punctuation">}</span>
	err <span class="token operator">=</span> user<span class="token punctuation">.</span><span class="token function">UpdateUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;更新数据错误,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;更新后的数据&quot;</span><span class="token punctuation">,</span> user<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><p><strong>其余更新操作再举例</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//这个更新操作只使用了where条件没有在Model中指定id</span>
	<span class="token comment">//等价于 update user set name='hello' wehre age=10</span>
	db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;AGE = ?&quot;</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Update</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">//多个条件写法 等价于 update user set name='hello' wehre age=10 and name='helloWorld'</span>
	db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;AGE = ?&quot;</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name=?&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;helloWorld&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Update</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">//等价于 update user set name='helloWorld' , age=18  使用 map 更新多个属性，只会更新其中有变化的属性</span>
	db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Updates</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;helloWorld&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;age&quot;</span><span class="token punctuation">:</span> <span class="token number">18</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h3 id="_7-2-更新所有字段"><a href="#_7-2-更新所有字段" class="header-anchor">#</a> 7.2 更新所有字段</h3> <blockquote><p><code>db.Save(&amp;user)</code>会更新所有字段，即使你没有赋值。一般建议使用<code>db.Model(&amp;user).Update(user)</code></p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token punctuation">(</span>user <span class="token operator">*</span>User<span class="token punctuation">)</span> <span class="token function">SaveUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span>err <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//Save会更新所有字段，即使你没有赋值</span>
	<span class="token comment">// 一般建议使用db.Model(&amp;user).Update(user)</span>
	update <span class="token operator">:=</span>db<span class="token punctuation">.</span><span class="token function">Save</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>
	err <span class="token operator">=</span> update<span class="token punctuation">.</span>Error
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;更新数据错误,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	user <span class="token operator">:=</span> User<span class="token punctuation">{</span>Id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> Name<span class="token punctuation">:</span> <span class="token string">&quot;蜡笔小新&quot;</span><span class="token punctuation">}</span>
	<span class="token comment">//等价于 UPDATE user SET name='蜡笔小新', age=0, create_time='0000-00-00 00:00:00' WHERE id=1;</span>
	user<span class="token punctuation">.</span><span class="token function">SaveUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h3 id="_7-3-更新选定字段"><a href="#_7-3-更新选定字段" class="header-anchor">#</a> 7.3 更新选定字段</h3> <blockquote><p>当你的更新的参数为结构体，而结构体中某些字段你又不想去更新，那么可以使用<code>Omit</code>方法过滤掉这些不想update到库的字段。
当你的更新的参数为结构体，你只想更新某些字段可以使用<code>Select</code>。举例如下:</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//定义需要更新的结构体</span>
	user <span class="token operator">:=</span> User<span class="token punctuation">{</span>Id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> Name<span class="token punctuation">:</span> <span class="token string">&quot;蜡笔小新&quot;</span><span class="token punctuation">,</span>Age<span class="token punctuation">:</span><span class="token number">222</span><span class="token punctuation">,</span>CreateTime<span class="token punctuation">:</span>time<span class="token punctuation">.</span><span class="token function">Now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">}</span>
	<span class="token comment">//更新时不对name字段进行修改</span>
    <span class="token comment">//等价于 UPDATE user SET  age=12 ,create_time= NOW() WHERE id=1;</span>
	db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Omit</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Update</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>
	<span class="token comment">//只对name字段进行修改</span>
    <span class="token comment">//等价于 UPDATE user SET name='蜡笔小新' WHERE id=1;</span>
	db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Update</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h3 id="_7-4-无-hooks-更新"><a href="#_7-4-无-hooks-更新" class="header-anchor">#</a> 7.4 无 Hooks 更新</h3> <blockquote><p>上面的更新操作会自动运行<code>model</code>的<code>BeforeUpdate</code>,<code>AfterUpdate</code>方法，更新<code>UpdatedAt</code>时间戳, 在更新时保存其<code>Associations</code>,如果你不想调用这些方法,你可以使用<code>UpdateColumn</code>,<code>UpdateColumns</code>。举例如下</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//定义需要更新的结构体</span>
	user <span class="token operator">:=</span> User<span class="token punctuation">{</span>Id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">}</span>
	<span class="token comment">// 更新单个属性，类似于 `Update` </span>
    <span class="token comment">//等价sql  UPDATE user SET name='hello' WHERE id = 1;</span>
	db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">UpdateColumn</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span>
	<span class="token comment">// 更新多个属性，类似于 `Updates`  </span>
    <span class="token comment">//等价sql  UPDATE user SET name='hello', age=18 WHERE id = 1;</span>
	db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">UpdateColumns</span><span class="token punctuation">(</span>User<span class="token punctuation">{</span>Name<span class="token punctuation">:</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">,</span> Age<span class="token punctuation">:</span> <span class="token number">18</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h3 id="_7-5-批量更新"><a href="#_7-5-批量更新" class="header-anchor">#</a> 7.5 批量更新</h3> <blockquote><p>批量更新时 Hooks 不会运行</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>

	<span class="token comment">//批量更新 等价于 UPDATE users SET name='hello', age=9000 WHERE id IN (1, 2);</span>
	db<span class="token punctuation">.</span><span class="token function">Table</span><span class="token punctuation">(</span><span class="token string">&quot;user&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;id IN (?)&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Updates</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;age&quot;</span><span class="token punctuation">:</span> <span class="token number">9000</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">// 使用 struct 更新时，只会更新非零值字段，若想更新所有字段，请使用map[string]interface{}</span>
	<span class="token comment">// UPDATE users SET name='hello', age=18;</span>
	db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span>User<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Updates</span><span class="token punctuation">(</span>User<span class="token punctuation">{</span>Name<span class="token punctuation">:</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">,</span> Age<span class="token punctuation">:</span> <span class="token number">18</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">// 使用 `RowsAffected` 获取更新记录总数</span>
	updateTotal <span class="token operator">:=</span> db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span>User<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Updates</span><span class="token punctuation">(</span>User<span class="token punctuation">{</span>Name<span class="token punctuation">:</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">,</span> Age<span class="token punctuation">:</span> <span class="token number">18</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>RowsAffected
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;更新的记录数量是&quot;</span><span class="token punctuation">,</span>updateTotal<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h3 id="_7-6-使用sql表达式更新"><a href="#_7-6-使用sql表达式更新" class="header-anchor">#</a> 7.6 使用SQL表达式更新</h3> <blockquote><p><code>gorm</code>提供了<code>Expr</code>函数用于设置表达式</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//定义需要更新的结构体</span>
	user <span class="token operator">:=</span> User<span class="token punctuation">{</span>Id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">}</span>
	<span class="token comment">//先计算年龄的值在执行更新操作</span>
	<span class="token comment">//UPDATE user SET age = age * '2' + '100' WHERE id = 1;</span>
	db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Update</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span> gorm<span class="token punctuation">.</span><span class="token function">Expr</span><span class="token punctuation">(</span><span class="token string">&quot;age * ? + ?&quot;</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

	<span class="token comment">//UPDATE user SET age = age * '2' + '100' WHERE id = 1;</span>
	db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Updates</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">{</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">:</span> gorm<span class="token punctuation">.</span><span class="token function">Expr</span><span class="token punctuation">(</span><span class="token string">&quot;age * ? + ?&quot;</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">//UPDATE user SET age = age - 10 WHERE id = 1;</span>
	db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">UpdateColumn</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span> gorm<span class="token punctuation">.</span><span class="token function">Expr</span><span class="token punctuation">(</span><span class="token string">&quot;age - ?&quot;</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

	<span class="token comment">//UPDATE user SET age = age - 10 WHERE id = 1 and age&gt;12;</span>
	db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;age &gt; 12&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">UpdateColumn</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span> gorm<span class="token punctuation">.</span><span class="token function">Expr</span><span class="token punctuation">(</span><span class="token string">&quot;age - ?&quot;</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h2 id="_8-删除数据"><a href="#_8-删除数据" class="header-anchor">#</a> 8 删除数据</h2> <h3 id="_8-1-删除记录"><a href="#_8-1-删除记录" class="header-anchor">#</a> 8.1 删除记录</h3> <div class="custom-block danger"><p class="custom-block-title">警告</p> <p>删除记录时,请确保主键字段有值,<code>GORM</code>会通过主键去删除记录,如果主键为空,<code>GORM</code>会删除该<code>model</code>的所有记录。</p></div> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token punctuation">(</span>user <span class="token operator">*</span>User<span class="token punctuation">)</span> <span class="token function">DeleteUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>err <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//Save会更新所有字段，即使你没有赋值</span>
	<span class="token comment">// 一般建议使用db.Model(&amp;user).Update(user)</span>
	deleteUser <span class="token operator">:=</span> db<span class="token punctuation">.</span><span class="token function">Delete</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>
	err <span class="token operator">=</span> deleteUser<span class="token punctuation">.</span>Error
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;删除数据错误,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//定义需要删除的结构体，主键字段必须有值</span>
	user <span class="token operator">:=</span> User<span class="token punctuation">{</span>Id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">}</span>
	<span class="token comment">//delete from user where id=1;</span>
	user<span class="token punctuation">.</span><span class="token function">DeleteUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h3 id="_8-2-批量删除"><a href="#_8-2-批量删除" class="header-anchor">#</a> 8.2 批量删除</h3> <blockquote><p>删除全部匹配的记录</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// DELETE from user where name LIKE &quot;%福小林%&quot;;</span>
	db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name LIKE ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;%福小林%&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Delete</span><span class="token punctuation">(</span>User<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">//DELETE from user where name LIKE &quot;%福小林%&quot;;</span>
	db<span class="token punctuation">.</span><span class="token function">Delete</span><span class="token punctuation">(</span>User<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">&quot;name LIKE ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;%福小林%&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h3 id="_8-3-物理删除"><a href="#_8-3-物理删除" class="header-anchor">#</a> 8.3 物理删除</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//定义需要删除的结构体，主键字段必须有值</span>
	user <span class="token operator">:=</span> User<span class="token punctuation">{</span>Id<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">}</span>
	<span class="token comment">// Unscoped 方法可以物理删除记录</span>
	<span class="token comment">//DELETE FROM user WHERE id=1;</span>
	db<span class="token punctuation">.</span><span class="token function">Unscoped</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Delete</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="_9-查询数据"><a href="#_9-查询数据" class="header-anchor">#</a> 9 查询数据</h2> <div class="custom-block tip"><p class="custom-block-title">gorm查询说明</p> <ul><li><code>gorm</code>查询数据本质上就是提供一组函数，帮我们快速拼接<code>sql</code>语句，尽量减少编写<code>sql</code>语句的工作量。</li> <li><code>gorm</code>查询结果我们一般都是保存到结构体<code>(struct)</code>变量，所以在执行查询操作之前需要根据自己想要查询的数据定义结构体类型。</li> <li><code>gorm</code>库是协程安全的，<code>gorm</code>提供的函数可以并发的在多个协程安全的执行。</li></ul></div> <h3 id="_9-1-查询方法"><a href="#_9-1-查询方法" class="header-anchor">#</a> 9.1 查询方法</h3> <blockquote><p>更多用法请参考官网<a href="https://gorm.io/zh_CN/docs/query.html" target="_blank" rel="noopener noreferrer">查询方法<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>,示例如下</p></blockquote> <table><thead><tr><th style="text-align:left;">操作含义</th> <th style="text-align:left;">golang示例</th> <th style="text-align:left;">等价sql</th></tr></thead> <tbody><tr><td style="text-align:left;">根据主键查询第一条记录</td> <td style="text-align:left;">db.First(&amp;user)</td> <td style="text-align:left;">SELECT * FROM user ORDER BY id LIMIT 1;</td></tr> <tr><td style="text-align:left;">随机获取一条记录</td> <td style="text-align:left;">db.Take(&amp;user)</td> <td style="text-align:left;">SELECT * FROM user LIMIT 1;</td></tr> <tr><td style="text-align:left;">根据主键查询最后一条记录</td> <td style="text-align:left;">db.Last(&amp;user)</td> <td style="text-align:left;">SELECT * FROM user ORDER BY id DESC LIMIT 1;</td></tr> <tr><td style="text-align:left;">查询所有的记录</td> <td style="text-align:left;">db.Find(&amp;users)</td> <td style="text-align:left;">SELECT * FROM user</td></tr> <tr><td style="text-align:left;">查询指定的某条记录,仅当<code>主键为整型</code>时可用</td> <td style="text-align:left;">db.First(&amp;user, 10)</td> <td style="text-align:left;">SELECT * FROM user WHERE id = 10;</td></tr></tbody></table> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//定义接收查询结果的结构体变量</span>
	user <span class="token operator">:=</span> User<span class="token punctuation">{</span><span class="token punctuation">}</span>

	<span class="token comment">//定义接收查询所有表结果的结构体切片</span>
	<span class="token keyword">var</span> users <span class="token punctuation">[</span><span class="token punctuation">]</span>User
	<span class="token comment">// 根据主键查询第一条记录  SELECT * FROM user ORDER BY id LIMIT 1;</span>
	db<span class="token punctuation">.</span><span class="token function">First</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>

	<span class="token comment">// 随机获取一条记录 SELECT * FROM user LIMIT 1;</span>
	db<span class="token punctuation">.</span><span class="token function">Take</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>

	<span class="token comment">// 根据主键查询最后一条记录 SELECT * FROM user ORDER BY id DESC LIMIT 1;</span>
	db<span class="token punctuation">.</span><span class="token function">Last</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>

	<span class="token comment">// 查询所有的记录 SELECT * FROM user</span>
	db<span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">// 查询指定的某条记录(仅当主键为整型时可用)</span>
	<span class="token comment">// SELECT * FROM user WHERE id = 3;</span>
	db<span class="token punctuation">.</span><span class="token function">First</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>

	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;查询的单条数据为&quot;</span><span class="token punctuation">,</span>user<span class="token punctuation">)</span>
	db<span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">,</span> <span class="token punctuation">)</span>
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> v <span class="token operator">:=</span> <span class="token keyword">range</span> users <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><h3 id="_9-2-查询错误处理"><a href="#_9-2-查询错误处理" class="header-anchor">#</a> 9.2 查询错误处理</h3> <blockquote><p>通过db.Error属性判断查询结果是否出错, Error属性不等于nil表示有错误发生。如下</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">if</span> err <span class="token operator">:=</span> db<span class="token punctuation">.</span><span class="token function">Take</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span><span class="token punctuation">.</span>Error<span class="token punctuation">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;查询失败&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="_9-2-1-错误特例"><a href="#_9-2-1-错误特例" class="header-anchor">#</a> 9.2.1 错误特例</h4> <blockquote><p>查询不到数据,<code>gorm</code>也会当成错误处理,如果查询不到数据,上面的例子会打印查询失败<code>record not found</code>
在实际开发中查询不到数据,我们不一定会当成错误处理,<code>gorm</code>库提供了下面两种办法检测<code>Error</code>是不是查询不到数据</p></blockquote> <h4 id="_9-2-2-gorm-isrecordnotfounderror"><a href="#_9-2-2-gorm-isrecordnotfounderror" class="header-anchor">#</a> 9.2.2 gorm.IsRecordNotFoundError</h4> <div class="language-go line-numbers-mode"><pre class="language-go"><code>err <span class="token operator">:=</span> db<span class="token punctuation">.</span><span class="token function">Take</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span><span class="token punctuation">.</span>Error
<span class="token keyword">if</span> gorm<span class="token punctuation">.</span><span class="token function">IsRecordNotFoundError</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;查询不到数据&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
<span class="token comment">//如果err不等于record not found错误，又不等于nil，那说明sql执行失败了。</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;查询失败&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h4 id="_9-2-3-db-recordnotfound"><a href="#_9-2-3-db-recordnotfound" class="header-anchor">#</a> 9.2.3 db.RecordNotFound</h4> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">//链式操作，先查询，然后检测查询结果</span>
<span class="token keyword">if</span> db<span class="token punctuation">.</span><span class="token function">Take</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span><span class="token punctuation">.</span>RecordNotFound <span class="token punctuation">{</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;查询不到数据&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
 <span class="token comment">//如果err不等于record not found错误，又不等于nil，那说明sql执行失败了。</span>
 	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;查询失败&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
 <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_9-3-where条件查询"><a href="#_9-3-where条件查询" class="header-anchor">#</a> 9.3 Where条件查询</h3> <blockquote><p>上面的例子都没有指定<code>where</code>条件,这里介绍下如何设置<code>where</code>条件,主要通过<code>db.Where</code>函数设置条件。函数说明如下：</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code> db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span>query <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>参数说明:</p> <table><thead><tr><th style="text-align:left;">参数名</th> <th style="text-align:left;">说明</th></tr></thead> <tbody><tr><td style="text-align:left;">query</td> <td style="text-align:left;">sql语句的where子句, where子句中使用问号(?)代替参数值，则表示通过args参数绑定参数</td></tr> <tr><td style="text-align:left;">args</td> <td style="text-align:left;">where子句绑定的参数，可以绑定多个参数</td></tr></tbody></table> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//定义接收查询结果的结构体变量</span>
	user <span class="token operator">:=</span> User<span class="token punctuation">{</span><span class="token punctuation">}</span>
	<span class="token comment">//定义接收查询所有表结果的结构体切片</span>
	<span class="token keyword">var</span> users <span class="token punctuation">[</span><span class="token punctuation">]</span>User

	<span class="token comment">// 获取第一条匹配的数据</span>
	<span class="token comment">// SELECT * FROM user WHERE name = 'hello' limit 1;</span>
	db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">First</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>

	<span class="token comment">// 获取所有匹配的数据，接受者是一个对应类型的切片[]User</span>
	<span class="token comment">// SELECT * FROM user WHERE name = 'hello';</span>
	db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">// SELECT * FROM user WHERE name &lt;&gt; 'hello';</span>
	db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name &lt;&gt; ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">// 拼接IN条件sql语句</span>
	<span class="token comment">// SELECT * FROM user WHERE name in ('hello','helloWorld');</span>
	db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name IN (?)&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;helloWorld&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">// 拼接LIKE模糊查询</span>
	<span class="token comment">// SELECT * FROM users WHERE name LIKE '%hel%';</span>
	db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name LIKE ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;%hel%&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">// AND条件拼接</span>
	<span class="token comment">// SELECT * FROM user WHERE name = 'hello' AND age &gt;= 22;</span>
	db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name = ? AND age &gt;= ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;22&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">// Time条件</span>
	<span class="token comment">// SELECT * FROM users WHERE create_time &gt; '2020-01-01 00:00:00';</span>
	db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;create_time &lt; ?&quot;</span><span class="token punctuation">,</span> time<span class="token punctuation">.</span><span class="token function">Now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">// BETWEEN</span>
	<span class="token comment">// SELECT * FROM user WHERE age BETWEEN 10 AND 20;</span>
	db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;age BETWEEN ? AND ?&quot;</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

    <span class="token comment">// Struct结构体 SELECT * FROM user WHERE name = &quot;xiaoxin&quot; AND age = 20 LIMIT 1;</span>
    db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span>Name<span class="token punctuation">:</span> <span class="token string">&quot;xiaoxin&quot;</span><span class="token punctuation">,</span> Age<span class="token punctuation">:</span> <span class="token number">20</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">First</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>

    <span class="token comment">// Map类型 SELECT * FROM user WHERE name = &quot;jinzhu&quot; AND age = 20;</span>
    db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;xiaoxin&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;age&quot;</span><span class="token punctuation">:</span> <span class="token number">20</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

    <span class="token comment">// 主键的切片 SELECT * FROM user WHERE id IN (20, 21, 22);</span>
    db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int64</span><span class="token punctuation">{</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">21</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br></div></div><p><strong>提示:当通过结构体进行查询时,<code>GORM</code>将会只通过非零值字段查询，这意味着如果你的字段值为<code>0,'',false或者其他零值</code>时，将不会被用于构建查询条件.例如</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// SELECT * FROM users WHERE name = &quot;xiaoxin&quot;;</span>
db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span>Name<span class="token punctuation">:</span> <span class="token string">&quot;xiaoxin&quot;</span><span class="token punctuation">,</span> Age<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><strong>你可以使用指针或实现<code>Scanner/Valuer</code> 接口来避免这个问题</strong></p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 使用指针</span>
<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token punctuation">{</span>
    Id         <span class="token builtin">int</span>    <span class="token string">`gorm:&quot;primary_key&quot;`</span>
	Name       <span class="token builtin">string</span> <span class="token string">`gorm:&quot;not null&quot;`</span>
	Age        <span class="token operator">*</span><span class="token builtin">int</span>
	CreateTime time<span class="token punctuation">.</span>Time <span class="token string">`gorm:&quot;column:create_time;not null&quot;`</span>
<span class="token punctuation">}</span>

<span class="token comment">// 使用 Scanner/Valuer</span>
<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token punctuation">{</span>
    Id         <span class="token builtin">int</span>    <span class="token string">`gorm:&quot;primary_key&quot;`</span>
  	Name       <span class="token builtin">string</span> <span class="token string">`gorm:&quot;not null&quot;`</span>
  	Age        sql<span class="token punctuation">.</span>NullInt64  <span class="token comment">// sql.NullInt64 实现了 Scanner/Valuer 接口</span>
  	CreateTime time<span class="token punctuation">.</span>Time <span class="token string">`gorm:&quot;column:create_time;not null&quot;`</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h3 id="_9-4-not条件"><a href="#_9-4-not条件" class="header-anchor">#</a> 9.4 Not条件</h3> <blockquote><p>作用与<code>Where</code>类似,举例如下</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//定义接收查询结果的结构体变量</span>
	user <span class="token operator">:=</span> User<span class="token punctuation">{</span><span class="token punctuation">}</span>
	<span class="token comment">//定义接收查询所有表结果的结构体切片</span>
	<span class="token keyword">var</span> users <span class="token punctuation">[</span><span class="token punctuation">]</span>User

	<span class="token comment">// SELECT * FROM user WHERE name &lt;&gt; &quot;xiaoxin&quot; LIMIT 1;</span>
	db<span class="token punctuation">.</span><span class="token function">Not</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;xiaoxin&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">First</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>

	<span class="token comment">// Not In 等价sql：SELECT * FROM user WHERE name NOT IN (&quot;hello&quot;, &quot;hello2&quot;);</span>
	db<span class="token punctuation">.</span><span class="token function">Not</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;hello&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;hello2&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">// Not In 在切片中的应用。主键的切片</span>
	<span class="token comment">// SELECT * FROM user WHERE id NOT IN (1,2,3)  LIMIT 1;</span>
	db<span class="token punctuation">.</span><span class="token function">Not</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int64</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">First</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>

	<span class="token comment">// SELECT * FROM user  LIMIT 1;</span>
	db<span class="token punctuation">.</span><span class="token function">Not</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int64</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">First</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>

	<span class="token comment">// SELECT * FROM user WHERE NOT(name = &quot;张三&quot;)  LIMIT 1;</span>
	db<span class="token punctuation">.</span><span class="token function">Not</span><span class="token punctuation">(</span><span class="token string">&quot;name = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">First</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>

	<span class="token comment">// Struct结构体  SELECT * FROM user WHERE name &lt;&gt; &quot;张三&quot;  LIMIT 1;</span>
	db<span class="token punctuation">.</span><span class="token function">Not</span><span class="token punctuation">(</span>User<span class="token punctuation">{</span>Name<span class="token punctuation">:</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">First</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>
	
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>users<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><h3 id="_9-5-or条件"><a href="#_9-5-or条件" class="header-anchor">#</a> 9.5 Or条件</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//定义接收查询所有表结果的结构体切片</span>
	<span class="token keyword">var</span> users <span class="token punctuation">[</span><span class="token punctuation">]</span>User

	<span class="token comment">// SELECT * FROM user WHERE name = '张三' OR name = '李四';</span>
	db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Or</span><span class="token punctuation">(</span><span class="token string">&quot;name = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;李四&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">// Struct结构体  // SELECT * FROM user WHERE name = '张三' OR name = '李四';</span>
	db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name = '张三'&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Or</span><span class="token punctuation">(</span>User<span class="token punctuation">{</span>Name<span class="token punctuation">:</span> <span class="token string">&quot;李四&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">// Map类型 SELECT * FROM user WHERE name = '张三' OR name = '李四';</span>
	db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name = '张三'&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Or</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;李四&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>users<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="_9-6-select子句"><a href="#_9-6-select子句" class="header-anchor">#</a> 9.6 select子句</h3> <blockquote><p>Select，指定你想从数据库中检索出的字段，默认会选择全部字段。举例如下</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//定义接收查询所有表结果的结构体切片</span>
	<span class="token keyword">var</span> users <span class="token punctuation">[</span><span class="token punctuation">]</span>User

	<span class="token comment">//查询指定字段，其他字段为每个类型的零值</span>
	<span class="token comment">// SELECT name, age FROM user;</span>
	db<span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;name, age&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">//查询指定字段，并添加where条件</span>
	<span class="token comment">//  SELECT name, age FROM user WHERE age=12;</span>
	db<span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;name, age&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">,</span><span class="token string">&quot;age=?&quot;</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">)</span>

	<span class="token comment">// SELECT name, create_time FROM user;</span>
	db<span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;create_time&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">// 指定表名和指定表字段查询 SELECT COALESCE(age,'42') FROM user;</span>
	db<span class="token punctuation">.</span><span class="token function">Table</span><span class="token punctuation">(</span><span class="token string">&quot;user&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;COALESCE(age,?)&quot;</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Rows</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h3 id="_9-7-order排序"><a href="#_9-7-order排序" class="header-anchor">#</a> 9.7 order排序</h3> <blockquote><p>设置排序语句,<code>order by</code>子句。指定从数据库中检索出记录的顺序。设置第二个参数<code>reorder</code>为<code>true</code>,可以覆盖前面定义的排序条件。</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token punctuation">(</span>s <span class="token operator">*</span>DB<span class="token punctuation">)</span> <span class="token function">Order</span><span class="token punctuation">(</span>value <span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> reorder <span class="token operator">...</span><span class="token builtin">bool</span><span class="token punctuation">)</span> <span class="token operator">*</span>DB <span class="token punctuation">{</span>
	<span class="token keyword">return</span> s<span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>search<span class="token punctuation">.</span><span class="token function">Order</span><span class="token punctuation">(</span>value<span class="token punctuation">,</span> reorder<span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">.</span>db
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//定义接收查询结果的结构体变量</span>
	<span class="token comment">//定义接收查询所有表结果的结构体切片</span>
	<span class="token keyword">var</span> users <span class="token punctuation">[</span><span class="token punctuation">]</span>User
	<span class="token keyword">var</span> users1 <span class="token punctuation">[</span><span class="token punctuation">]</span>User
	<span class="token keyword">var</span> users2 <span class="token punctuation">[</span><span class="token punctuation">]</span>User

	<span class="token comment">// SELECT * FROM user ORDER BY age desc;</span>
	db<span class="token punctuation">.</span><span class="token function">Order</span><span class="token punctuation">(</span><span class="token string">&quot;age desc&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">// 多字段排序 SELECT * FROM user ORDER BY age desc, name;</span>
	db<span class="token punctuation">.</span><span class="token function">Order</span><span class="token punctuation">(</span><span class="token string">&quot;age desc&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Order</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">// 覆盖排序</span>
	<span class="token comment">//// SELECT * FROM user ORDER BY age desc; (users1)</span>
	<span class="token comment">//// SELECT * FROM user ORDER BY age; (users2)</span>
	db<span class="token punctuation">.</span><span class="token function">Order</span><span class="token punctuation">(</span><span class="token string">&quot;age desc&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users1<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Order</span><span class="token punctuation">(</span><span class="token string">&quot;age&quot;</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users2<span class="token punctuation">)</span>
	<span class="token comment">//[{3 王五 78 2020-02-05 16:37:58 +0800 CST} {4 赵六 56 2020-02-05 16:37:58 +0800 CST} {2 李四 24 2020-02-05 16:37:58 +0800 CST} {1 张三 12 2020-02-05 16:37:58 +0800 CST}]</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>users1<span class="token punctuation">)</span>
	<span class="token comment">//[{1 张三 12 2020-02-05 16:37:58 +0800 CST} {2 李四 24 2020-02-05 16:37:58 +0800 CST} {4 赵六 56 2020-02-05 16:37:58 +0800 CST} {3 王五 78 2020-02-05 16:37:58 +0800 CST}]</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>users2<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><h3 id="_9-8-limit-count"><a href="#_9-8-limit-count" class="header-anchor">#</a> 9.8 Limit &amp; Count</h3> <blockquote><p><code>Limit</code>指定从数据库检索出的最大记录数。<code>Count</code>该<code>model</code>能获取的记录总数。</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//定义接收查询所有表结果的结构体切片</span>
	<span class="token keyword">var</span> users <span class="token punctuation">[</span><span class="token punctuation">]</span>User
	<span class="token keyword">var</span> users1 <span class="token punctuation">[</span><span class="token punctuation">]</span>User
	<span class="token keyword">var</span> users2 <span class="token punctuation">[</span><span class="token punctuation">]</span>User
	<span class="token keyword">var</span> count <span class="token builtin">int</span> 

	<span class="token comment">// SELECT * FROM user LIMIT 3;</span>
	db<span class="token punctuation">.</span><span class="token function">Limit</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span>

	<span class="token comment">// -1 取消 Limit 条件</span>
	<span class="token comment">// SELECT * FROM user LIMIT 10; (users1)</span>
	<span class="token comment">// SELECT * FROM user; (users2)</span>
	db<span class="token punctuation">.</span><span class="token function">Limit</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users1<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Limit</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users2<span class="token punctuation">)</span>

	<span class="token comment">// SELECT * from user WHERE name = '张三' OR name = '李四'; (users)</span>
	<span class="token comment">// SELECT count(*) FROM user WHERE name = '张三' OR name = '李四'; (count)</span>
	db<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Or</span><span class="token punctuation">(</span><span class="token string">&quot;name = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;李四&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>users<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Count</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>count<span class="token punctuation">)</span>

	<span class="token comment">// SELECT count(*) FROM user WHERE name = '张三'; (count)</span>
	db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Count</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>count<span class="token punctuation">)</span>

	<span class="token comment">//指定表名 SELECT count(*) FROM user;</span>
	db<span class="token punctuation">.</span><span class="token function">Table</span><span class="token punctuation">(</span><span class="token string">&quot;user&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Count</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>count<span class="token punctuation">)</span>

	<span class="token comment">// 统计有条件 SELECT count( distinct(name) ) FROM user; (count)</span>
	db<span class="token punctuation">.</span><span class="token function">Table</span><span class="token punctuation">(</span><span class="token string">&quot;user&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;count(distinct(name))&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Count</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>count<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><h3 id="_9-9-group-having"><a href="#_9-9-group-having" class="header-anchor">#</a> 9.9 Group &amp; Having</h3> <div class="custom-block tip"><p class="custom-block-title">注意点</p> <ul><li><code>Group</code>函数必须搭配<code>Select</code>函数一起使用</li> <li>下面例子<code>scan</code>类似<code>Find</code>都是用于执行查询语句,然后把查询结果赋值给结构体变量,区别在于<code>scan</code>不会从传递进来的结构体变量提取表名.</li> <li>下面例子重新定义了一个结构体用于保存结果,但是这个结构体并没有绑定<code>user</code>表,所以这里只能使用<code>scan</code>查询函数。</li></ul></div> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token comment">//定一个Result结构体类型，用来保存查询结果</span>
	<span class="token keyword">type</span> Result <span class="token keyword">struct</span> <span class="token punctuation">{</span>
		Date  time<span class="token punctuation">.</span>Time
		Total <span class="token builtin">int</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// SELECT date(create_time) as date, sum(age) as total FROM user GROUP BY date(create_time)</span>
	rows<span class="token punctuation">,</span> err <span class="token operator">:=</span> db<span class="token punctuation">.</span><span class="token function">Table</span><span class="token punctuation">(</span><span class="token string">&quot;user&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;date(create_time) as date, sum(age) as total&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Group</span><span class="token punctuation">(</span><span class="token string">&quot;date(create_time)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Rows</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">for</span> rows<span class="token punctuation">.</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">var</span> result Result
		err <span class="token operator">:=</span> rows<span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>result<span class="token punctuation">.</span>Date<span class="token punctuation">,</span><span class="token operator">&amp;</span>result<span class="token punctuation">.</span>Total<span class="token punctuation">)</span>
		<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span><span class="token punctuation">{</span>
			fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;scan失败,err&quot;</span><span class="token punctuation">,</span>err<span class="token punctuation">)</span>
			<span class="token keyword">return</span>
		<span class="token punctuation">}</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
		<span class="token comment">//{2020-02-01 00:00:00 +0800 CST 24}</span>
		<span class="token comment">//{2020-02-05 00:00:00 +0800 CST 146}</span>
	<span class="token punctuation">}</span>

	<span class="token comment">//SELECT date(create_time) as date, sum(age) as total FROM user GROUP BY date(create_time) HAVING (sum(age)&gt;100)</span>
	rows<span class="token punctuation">,</span> err <span class="token operator">=</span> db<span class="token punctuation">.</span><span class="token function">Table</span><span class="token punctuation">(</span><span class="token string">&quot;user&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;date(create_time) as date, sum(age) as total&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Group</span><span class="token punctuation">(</span><span class="token string">&quot;date(create_time)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Having</span><span class="token punctuation">(</span><span class="token string">&quot;sum(age) &gt; ?&quot;</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Rows</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">for</span> rows<span class="token punctuation">.</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">//……取值操作请参考14--24行</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 下面两种方式类似</span>
	<span class="token comment">//scan类似Find都是用于执行查询语句，然后把查询结果赋值给结构体变量，区别在于scan不会从传递进来的结构体变量提取表名.</span>
	<span class="token comment">//这里因为我们重新定义了一个结构体用于保存结果，但是这个结构体并没有绑定user表，所以这里只能使用scan查询函数。</span>
	<span class="token keyword">var</span> results <span class="token punctuation">[</span><span class="token punctuation">]</span>Result
	<span class="token comment">//SELECT date(create_time) as date, sum(age) as total FROM user GROUP BY date(create_time) HAVING (sum(age)&gt;100)</span>
	db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span>User<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;date(create_time) as date, sum(age) as total&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Group</span><span class="token punctuation">(</span><span class="token string">&quot;date(create_time)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Having</span><span class="token punctuation">(</span><span class="token string">&quot;sum(age) &gt; ?&quot;</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>results<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>results<span class="token punctuation">)</span> <span class="token comment">//[{2020-02-01 00:00:00 +0800 CST 24}]</span>

	db<span class="token punctuation">.</span><span class="token function">Table</span><span class="token punctuation">(</span><span class="token string">&quot;user&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;date(create_time) as date, sum(age) as total&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Group</span><span class="token punctuation">(</span><span class="token string">&quot;date(create_time)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Having</span><span class="token punctuation">(</span><span class="token string">&quot;sum(age) &gt; ?&quot;</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>results<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>results<span class="token punctuation">)</span> <span class="token comment">//[{2020-02-01 00:00:00 +0800 CST 24}]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br></div></div><h3 id="_9-10-连接join"><a href="#_9-10-连接join" class="header-anchor">#</a> 9.10 连接join</h3> <blockquote><p>Joins，指定连接条件</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">type</span> Result <span class="token keyword">struct</span> <span class="token punctuation">{</span>
		Name  <span class="token builtin">string</span>
		Email <span class="token builtin">string</span>
	<span class="token punctuation">}</span>

	<span class="token comment">//SELECT users.name, emails.email FROM users LEFT JOIN emails on emails.user_id = users.id</span>
	rows<span class="token punctuation">,</span> err <span class="token operator">:=</span> db<span class="token punctuation">.</span><span class="token function">Table</span><span class="token punctuation">(</span><span class="token string">&quot;users&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;users.name, emails.email&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Joins</span><span class="token punctuation">(</span><span class="token string">&quot;left join emails on emails.user_id = users.id&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Rows</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">for</span> rows<span class="token punctuation">.</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token comment">//...</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">var</span> results <span class="token punctuation">[</span><span class="token punctuation">]</span>Result
	<span class="token comment">//SELECT users.name, emails.email FROM users LEFT JOIN emails on emails.user_id = users.id</span>
	db<span class="token punctuation">.</span><span class="token function">Table</span><span class="token punctuation">(</span><span class="token string">&quot;users&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;users.name, emails.email&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Joins</span><span class="token punctuation">(</span><span class="token string">&quot;left join emails on emails.user_id = users.id&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>results<span class="token punctuation">)</span>

	<span class="token keyword">var</span> user User
	<span class="token comment">// 多连接及参数</span>
	db<span class="token punctuation">.</span><span class="token function">Joins</span><span class="token punctuation">(</span><span class="token string">&quot;JOIN emails ON emails.user_id = users.id AND emails.email = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;ourlang@example.org&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Joins</span><span class="token punctuation">(</span><span class="token string">&quot;JOIN credit_cards ON credit_cards.user_id = users.id&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;credit_cards.number = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;411111111111&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Find</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h3 id="_9-11-直接执行sql语句"><a href="#_9-11-直接执行sql语句" class="header-anchor">#</a> 9.11 直接执行sql语句</h3> <blockquote><p>对于复杂的查询,例如多表连接查询,我们可以直接编写<code>sql</code>语句,然后执行<code>sql</code>语句。
<code>gorm</code>通过<code>db.Raw</code>设置<code>sql</code>语句,通过<code>Scan</code>执行查询</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">type</span> Result <span class="token keyword">struct</span> <span class="token punctuation">{</span>
		Name  <span class="token builtin">string</span>
		Age   <span class="token builtin">int</span>
		Total <span class="token builtin">int</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">var</span> result <span class="token punctuation">[</span><span class="token punctuation">]</span>Result
	db<span class="token punctuation">.</span><span class="token function">Table</span><span class="token punctuation">(</span><span class="token string">&quot;user&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;name, age&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;赵六&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>result<span class="token punctuation">)</span>

	<span class="token comment">// 原生 SQL</span>
	db<span class="token punctuation">.</span><span class="token function">Raw</span><span class="token punctuation">(</span><span class="token string">&quot;SELECT name, age FROM user WHERE name = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;赵六&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>result<span class="token punctuation">)</span>
	sqlStr <span class="token operator">:=</span> <span class="token string">&quot;SELECT name , COUNT(*) AS total FROM user WHERE create_time&gt;? GROUP BY name HAVING(total &gt;0)&quot;</span>

	<span class="token comment">//因为sql语句使用了一个问号(?)作为绑定参数, 所以需要传递一个绑定参数(Raw第二个参数).</span>
	<span class="token comment">//Raw函数支持绑定多个参数</span>
	db<span class="token punctuation">.</span><span class="token function">Raw</span><span class="token punctuation">(</span>sqlStr<span class="token punctuation">,</span> <span class="token string">&quot;2019-01-01 00:00:00&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>result<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h2 id="_10-sql生成器"><a href="#_10-sql生成器" class="header-anchor">#</a> 10 SQL生成器</h2> <h3 id="_10-1-执行原生sql"><a href="#_10-1-执行原生sql" class="header-anchor">#</a> 10.1 执行原生SQL</h3> <blockquote><p>执行原生SQL时,不支持与其它方法的链式操作。更多用法请参考官网<a href="https://gorm.io/zh_CN/docs/sql_builder.html" target="_blank" rel="noopener noreferrer">SQL生成器<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>

	<span class="token comment">//修改 新增 删除用db.Exec()</span>
	db<span class="token punctuation">.</span><span class="token function">Exec</span><span class="token punctuation">(</span><span class="token string">&quot;DROP TABLE user;&quot;</span><span class="token punctuation">)</span>
	db<span class="token punctuation">.</span><span class="token function">Exec</span><span class="token punctuation">(</span><span class="token string">&quot;UPDATE user SET create_time=? WHERE id IN (?)&quot;</span><span class="token punctuation">,</span> time<span class="token punctuation">.</span><span class="token function">Now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">int64</span><span class="token punctuation">{</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">22</span><span class="token punctuation">,</span><span class="token number">33</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

	<span class="token comment">// Scan</span>
	<span class="token keyword">type</span> Result <span class="token keyword">struct</span> <span class="token punctuation">{</span>
		Name <span class="token builtin">string</span>
		Age  <span class="token builtin">int</span>
	<span class="token punctuation">}</span>

	<span class="token comment">//查询使用db.Raw()</span>
	<span class="token keyword">var</span> result<span class="token punctuation">[</span><span class="token punctuation">]</span> Result
	db<span class="token punctuation">.</span><span class="token function">Raw</span><span class="token punctuation">(</span><span class="token string">&quot;SELECT name, age FROM user WHERE id &gt; ?&quot;</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>result<span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h3 id="_10-2-sql-row和sql-rows"><a href="#_10-2-sql-row和sql-rows" class="header-anchor">#</a> 10.2 sql.Row和sql.Rows</h3> <blockquote><p>通过<code>*sql.Row</code>或<code>*sql.Rows</code> 获取查询结果</p></blockquote> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">var</span> user User
	row <span class="token operator">:=</span> db<span class="token punctuation">.</span><span class="token function">Table</span><span class="token punctuation">(</span><span class="token string">&quot;user&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;name, age&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Row</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// (*sql.Row)</span>
	row<span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>user<span class="token punctuation">.</span>Name<span class="token punctuation">,</span> <span class="token operator">&amp;</span>user<span class="token punctuation">.</span>Age<span class="token punctuation">)</span>

	rows<span class="token punctuation">,</span> err <span class="token operator">:=</span> db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;name, age&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Rows</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// (*sql.Rows, error)</span>
	<span class="token keyword">defer</span> rows<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">for</span> rows<span class="token punctuation">.</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">var</span> u User
		rows<span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>u<span class="token punctuation">.</span>Name<span class="token punctuation">,</span> <span class="token operator">&amp;</span>u<span class="token punctuation">.</span>Age<span class="token punctuation">)</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>u<span class="token punctuation">.</span>Name<span class="token punctuation">,</span>u<span class="token punctuation">.</span>Age<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>

	<span class="token comment">// 原生 SQL</span>
	rows<span class="token punctuation">,</span> err <span class="token operator">=</span> db<span class="token punctuation">.</span><span class="token function">Raw</span><span class="token punctuation">(</span><span class="token string">&quot;select name, age  from user where name = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Rows</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// (*sql.Rows, error)</span>
	<span class="token keyword">defer</span> rows<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">for</span> rows<span class="token punctuation">.</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">var</span> u User
		rows<span class="token punctuation">.</span><span class="token function">Scan</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>u<span class="token punctuation">.</span>Name<span class="token punctuation">,</span> <span class="token operator">&amp;</span>u<span class="token punctuation">.</span>Age<span class="token punctuation">)</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>u<span class="token punctuation">.</span>Name<span class="token punctuation">,</span>u<span class="token punctuation">.</span>Age<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><h3 id="_10-3-将sql-rows扫描至model"><a href="#_10-3-将sql-rows扫描至model" class="header-anchor">#</a> 10.3 将sql.Rows扫描至model</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	rows<span class="token punctuation">,</span> err <span class="token operator">:=</span> db<span class="token punctuation">.</span><span class="token function">Model</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token string">&quot;name = ?&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token string">&quot;name, age&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Rows</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// (*sql.Rows, error)</span>
	<span class="token keyword">defer</span> rows<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token keyword">for</span> rows<span class="token punctuation">.</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">var</span> user User
		<span class="token comment">// ScanRows 扫描一行记录到 user</span>
		db<span class="token punctuation">.</span><span class="token function">ScanRows</span><span class="token punctuation">(</span>rows<span class="token punctuation">,</span> <span class="token operator">&amp;</span>user<span class="token punctuation">)</span>
		<span class="token comment">// do something</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>user<span class="token punctuation">.</span>Name<span class="token punctuation">,</span>user<span class="token punctuation">.</span>Age<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h2 id="_11-事务处理"><a href="#_11-事务处理" class="header-anchor">#</a> 11 事务处理</h2> <blockquote><p><code>GORM</code>默认会将单个的<code>create, update, delete</code>操作封装在事务内进行处理,以确保数据的完整性。
如果你想把多个<code>create, update, delete</code>操作作为一个原子操作,<code>Transaction</code>就是用来完成这个的。
在<code>MySQL</code>中只有使用了<code>Innodb</code>数据库引擎的数据库或表才支持事务</p></blockquote> <h3 id="_11-1-事务流程说明"><a href="#_11-1-事务流程说明" class="header-anchor">#</a> 11.1 事务流程说明</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">CreateUsers</span><span class="token punctuation">(</span>db <span class="token operator">*</span>gorm<span class="token punctuation">.</span>DB<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> db<span class="token punctuation">.</span><span class="token function">Transaction</span><span class="token punctuation">(</span><span class="token keyword">func</span><span class="token punctuation">(</span>tx <span class="token operator">*</span>gorm<span class="token punctuation">.</span>DB<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
    <span class="token comment">// 在事务中做一些数据库操作 (这里应该使用 'tx' ，而不是 'db')</span>
    <span class="token keyword">if</span> err <span class="token operator">:=</span> tx<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span>Name<span class="token punctuation">:</span> <span class="token string">&quot;zhangsan&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Error<span class="token punctuation">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
      <span class="token comment">// 返回任意 err ，整个事务都会 rollback</span>
      <span class="token keyword">return</span> err
    <span class="token punctuation">}</span>

    <span class="token keyword">if</span> err <span class="token operator">:=</span> tx<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span>Name<span class="token punctuation">:</span> <span class="token string">&quot;lisi&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Error<span class="token punctuation">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> err
    <span class="token punctuation">}</span>

    <span class="token comment">// 返回 nil 提交事务</span>
    <span class="token keyword">return</span> <span class="token boolean">nil</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h3 id="_11-2-事务（手动控制）"><a href="#_11-2-事务（手动控制）" class="header-anchor">#</a> 11.2 事务（手动控制）</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 开启事务</span>
tx <span class="token operator">:=</span> db<span class="token punctuation">.</span><span class="token function">Begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 在事务中做一些数据库操作 (这里应该使用 'tx' ，而不是 'db')</span>
tx<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span>

<span class="token comment">// ...</span>

<span class="token comment">// 有错误时，手动调用事务的 Rollback()</span>
tx<span class="token punctuation">.</span><span class="token function">Rollback</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 无错误时，手动调用事务的 Commit()</span>
tx<span class="token punctuation">.</span><span class="token function">Commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h3 id="_11-3-事务示例"><a href="#_11-3-事务示例" class="header-anchor">#</a> 11.3 事务示例</h3> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;fmt&quot;</span>
	<span class="token string">&quot;github.com/jinzhu/gorm&quot;</span>
	<span class="token boolean">_</span> <span class="token string">&quot;github.com/jinzhu/gorm/dialects/mysql&quot;</span>
	<span class="token string">&quot;time&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">var</span> <span class="token punctuation">(</span>
	<span class="token comment">// 定义一个全局对象db</span>
	db <span class="token operator">*</span>gorm<span class="token punctuation">.</span>DB
	<span class="token comment">//定义数据库连接的相关参数值</span>
	<span class="token comment">//连接数据库的用户名</span>
	userName <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;root&quot;</span>
	<span class="token comment">//连接数据库的密码</span>
	password <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;admin&quot;</span>
	<span class="token comment">//连接数据库的地址</span>
	ipAddress <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;127.0.0.1&quot;</span>
	<span class="token comment">//连接数据库的端口号</span>
	port <span class="token builtin">int</span> <span class="token operator">=</span> <span class="token number">3306</span>
	<span class="token comment">//连接数据库的具体数据库名称</span>
	dbName <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;go_test&quot;</span>
	<span class="token comment">//连接数据库的编码格式</span>
	charset <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;utf8mb4&quot;</span>
	<span class="token comment">//想要能正确的处理 time.Time，你需要添加 parseTime 参数。</span>
	parseTime <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;True&quot;</span>
	<span class="token comment">//设置时间的位置</span>
	loc <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">&quot;Local&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>err <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//dsn=&quot;root:admin@tcp(127.0.0.1:3306)/go_test?charset=utf8mb4&amp;parseTime=True&amp;loc=Local&quot;</span>
	dsn <span class="token operator">:=</span> fmt<span class="token punctuation">.</span><span class="token function">Sprintf</span><span class="token punctuation">(</span><span class="token string">&quot;%s:%s@tcp(%s:%d)/%s?charset=%s&amp;parseTime=%s&amp;loc=%s&quot;</span><span class="token punctuation">,</span> userName<span class="token punctuation">,</span> password<span class="token punctuation">,</span> ipAddress<span class="token punctuation">,</span> port<span class="token punctuation">,</span> dbName<span class="token punctuation">,</span> charset<span class="token punctuation">,</span> parseTime<span class="token punctuation">,</span> loc<span class="token punctuation">)</span>
	db<span class="token punctuation">,</span> err <span class="token operator">=</span> gorm<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span><span class="token string">&quot;mysql&quot;</span><span class="token punctuation">,</span> dsn<span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;连接数据库异常,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>
	<span class="token comment">//设置最大的空闲连接数</span>
	db<span class="token punctuation">.</span><span class="token function">DB</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">SetMaxIdleConns</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
	<span class="token comment">// 设置最大连接数</span>
	db<span class="token punctuation">.</span><span class="token function">DB</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">SetMaxOpenConns</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span>
	<span class="token comment">//自动生成数据库表 ，生成的表明默认是struct名称的复数形式。如：user -》users</span>
	<span class="token comment">// 如果不想是复数形式，需设定：</span>
	db<span class="token punctuation">.</span><span class="token function">SingularTable</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
	<span class="token comment">//defer db.Close()</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;连接数据库成功！&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>

<span class="token comment">//创建一个和数据库表结构一样的机构体</span>
<span class="token comment">//gorm 用 tag 的方式来标识 mysql 里面的约束</span>
<span class="token keyword">type</span> User <span class="token keyword">struct</span> <span class="token punctuation">{</span>
	Id         <span class="token builtin">int</span>    <span class="token string">`gorm:&quot;primary_key&quot;`</span>
	Name       <span class="token builtin">string</span> <span class="token string">`gorm:&quot;not null&quot;`</span>
	Age        <span class="token builtin">int</span>
	CreateTime time<span class="token punctuation">.</span>Time <span class="token string">`gorm:&quot;column:create_time;not null&quot;`</span>
<span class="token punctuation">}</span>

<span class="token comment">//CreateUsers 批量插入用户数据</span>
<span class="token comment">//如果一个报错，则回滚已经做过的操作</span>
<span class="token keyword">func</span> <span class="token function">CreateUsers</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
	<span class="token comment">// 请注意，事务一旦开始，你就应该使用 tx 作为数据库句柄</span>
	tx <span class="token operator">:=</span> db<span class="token punctuation">.</span><span class="token function">Begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">defer</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">if</span> r <span class="token operator">:=</span> <span class="token function">recover</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> r <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
			tx<span class="token punctuation">.</span><span class="token function">Rollback</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	<span class="token keyword">if</span> err <span class="token operator">:=</span> tx<span class="token punctuation">.</span>Error<span class="token punctuation">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>

	<span class="token keyword">if</span> err <span class="token operator">:=</span> tx<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span>Name<span class="token punctuation">:</span> <span class="token string">&quot;zhangsan&quot;</span><span class="token punctuation">,</span>Age<span class="token punctuation">:</span><span class="token number">17</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Error<span class="token punctuation">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		tx<span class="token punctuation">.</span><span class="token function">Rollback</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>

	<span class="token keyword">if</span> err <span class="token operator">:=</span> tx<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>User<span class="token punctuation">{</span>Name<span class="token punctuation">:</span> <span class="token string">&quot;lisi&quot;</span><span class="token punctuation">,</span>Age<span class="token punctuation">:</span><span class="token number">38</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Error<span class="token punctuation">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		tx<span class="token punctuation">.</span><span class="token function">Rollback</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		<span class="token keyword">return</span> err
	<span class="token punctuation">}</span>

	<span class="token keyword">return</span> tx<span class="token punctuation">.</span><span class="token function">Commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Error
<span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	err <span class="token operator">:=</span> <span class="token function">connectMysql</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;初始化数据库失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
	err <span class="token operator">=</span> <span class="token function">CreateUsers</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;事务操作失败,err&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
		<span class="token keyword">return</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br><span class="line-number">97</span><br><span class="line-number">98</span><br><span class="line-number">99</span><br></div></div><h2 id="_12-gorm更多资料"><a href="#_12-gorm更多资料" class="header-anchor">#</a> 12 GORM更多资料</h2> <p><a href="https://gorm.io/zh_CN/docs/" target="_blank" rel="noopener noreferrer">GORM中文文档<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/go/middleware/goproxy.html" class="prev">
        GoLand配置GOPROXY代理
      </a></span> <span class="next"><a href="/go/middleware/json-iterator.html">
        json-iterator使用
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.1bc80adb.js" defer></script><script src="/assets/js/2.7c0608ab.js" defer></script><script src="/assets/js/95.1b379a5b.js" defer></script>
  </body>
</html>
